package com.bite.springbook.Service;

import com.bite.springbook.Dao.BookDao;
import com.bite.springbook.Model.*;
import com.bite.springbook.mapper.BookMapper;
import com.bite.springbook.mapper.UserInfoMapper;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookDao bookDao;
    public List<BookInfo> getBookList(){

        List<BookInfo> books = bookDao.mockData();
        for(BookInfo book : books){
            if(book.getStatus() == 2){
                book.setStatusCN("可借阅");
            }else{
                book.setStatusCN("不可借阅");
            }
        }
        return books;
        
    }

    public Integer addBook(BookInfo bookInfo){
        return bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.queryBookListByPage(pageRequest);
        for(BookInfo book : bookInfos){
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getDesc());
        }
        return new PageResult<>(count, bookInfos, pageRequest);

    }


    public BookInfo getBookById(Integer id) {
        return bookMapper.queryBookById(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(0);
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}
